<?php include '../../authorization_check.php' ?>
<?php include '../../checkSecretaryOnly.php' ?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-9">
<title>Edit staff details</title>
<SCRIPT TYPE="text/javascript" src= "../../commonutils/util/util.js"> </SCRIPT>
</head>
<style type="text/css">
th {text-align: right}
</style>
<body>
<?php include 'top.html' ?>

<?php
  session_start();
  $programCode = $_SESSION['programCode'];
  $semesterID = $_SESSION['semesterID'];
  
?>

<?php
require_once '../../commonutils/db/DBConnectionManager.php';

$personnelID = $_POST['personnelID'];


if (isset($_POST['btnSubmit']))
{
	
	$personnelName = strtoupper($_POST['personnelName']);
	$personnelSurname = strtoupper($_POST['personnelSurname']);
	$citizenship = $_POST['citizenship'];
	
	$bdd = $_POST['bdd'];
	$bmm = $_POST['bmm'];
	$byyyy = $_POST['byyyy'];
	
	$dateOfBirth = $byyyy."-".$bmm."-".$bdd;
	$placeOfBirth = strtoupper($_POST['placeOfBirth']);
	

	$edd = $_POST['edd'];
	$emm = $_POST['emm'];
	$eyyyy = $_POST['eyyyy'];
	$employmentDate = $eyyyy."-".$emm."-".$edd;
	
	$title = $_POST['title'];
	
	if($title == "Assistant" || $title == "Administrative" ){
		$sqlquery = "update personnel set firstname = '".$personnelName."' ".
							", lastname = '".$personnelSurname."' ".
							", title = '".$title."' ".
							", TC_Kimlik = ".$citizenship.
							", dateOfBirth = '".$dateOfBirth."' ".
							", placeOfBirth = '".$placeOfBirth."' ".
							", employmentDate = '".$employmentDate."' ".
							" where personnelID = ".$personnelID;

							
		$result = DBConnectionManager::query($sqlquery);		
	}
	else
	{
		$sqlquery = "update instructor set firstname = '".$personnelName."'".
							", lastname = '".$personnelSurname."' ".
							", title = '".$title."' ".
							", TC_Kimlik = ".$citizenship.
							", dateOfBirth = '".$dateOfBirth."' ".
							", placeOfBirth = '".$placeOfBirth."' ".
							", employmentDate = '".$employmentDate."' ".
							" where instructorID = ".$personnelID;
		$result = DBConnectionManager::query($sqlquery);
		
		
	}
	
	echo "<p><font color=blue><b>You have successfully edited ".$personnelName." ".$personnelSurname." details.</b></font></p>";
	//echo $sqlquery."<BR>"; 
	
	//$row = mysql_fetch_assoc($result);
}
 $sqlquery = "  select count(*) as cnt from ".
  			 " (select firstname from instructor where instructorID = ".$personnelID.
 			 " union ".	
 			 " select firstname from personnel where personnelID = ".$personnelID.")".
 			 " as tableunion ";
  

  //echo $sqlquery."<br>";
 $result = DBConnectionManager::query($sqlquery);
 $row = mysql_fetch_assoc($result);
 
 $cnt = $row['cnt'];
 //echo $cnt."<br>";
 
 if($cnt != 1)
   header('Location: SearchStaff.php') ;
  
 $sqlquery = " select firstname, lastname, title, TC_Kimlik, dateOfBirth, placeOfBirth, employmentDate from instructor where instructorID = ".$personnelID.
 			 " union ".	
 			 " select firstname, lastname, title, TC_Kimlik, dateOfBirth, placeOfBirth, employmentDate from personnel where personnelID = ".$personnelID;
 

 //// echo $sqlquery."<br>";
 $result = DBConnectionManager::query($sqlquery);
 $row = mysql_fetch_assoc($result);
 $personnelName = $row['firstname'];
 $personnelSurname = $row['lastname'];
 $title = $row['title'];
 $citizenship = $row['TC_Kimlik'];
 $dateOfBirth = $row['dateOfBirth'];
 $byyyy = substr($dateOfBirth, 0, 4);
 $bmm = substr($dateOfBirth, 5, 2);
 $bdd = substr($dateOfBirth, 8, 2);
 $placeOfBirth = $row['placeOfBirth'];	
 $employmentDate = $row['employmentDate'];
 $eyyyy = substr($employmentDate, 0, 4);
 $emm = substr($employmentDate, 5, 2);
 $edd = substr($employmentDate, 8, 2); 
?>
  
<FORM NAME ="form1" METHOD ="POST" ACTION = "EditStaff.php">
<table>
<tr>
<td>Personnel ID: </td>
<td><input readonly=readonly  type="text" name="personnelID" size="15"  value="<?php echo $personnelID ?>" onKeyPress="return numbersonly(this, event)"/></td>
</tr>
<td>Name: </td>
<td><input type="text" style="text-transform: uppercase" name="personnelName" size="20" value="<?php echo $personnelName ?>"/></td>
</tr>
<td>Surname: </td>
<td><input type="text" style="text-transform: uppercase" name="personnelSurname" size="20" value="<?php echo $personnelSurname ?>"/></td>
</tr>
<td>Citizenship Number: </td>
<td><input type="text" name="citizenship" size="15" maxlength=11 onKeyPress="return numbersonly(this, event)" value="<?php echo $citizenship ?>"/></td>
</tr>
<td>Date of birth (DD/MM/YYYY): </td>
<td>
<input type="text" name="bdd" size="2" value="<?php echo $bdd ?>" onKeyPress="return numbersonly(this, event)"/> /
<input type="text" name="bmm" size="2" value="<?php echo $bmm ?>" onKeyPress="return numbersonly(this, event)"/> /
<input type="text" name="byyyy" size="4" value="<?php echo $byyyy ?>" onKeyPress="return numbersonly(this, event)"/>
</td>
</tr>
<td>Place of birth: </td>
<td><input type="text" name="placeOfBirth" size="20" value="<?php echo $placeOfBirth ?>"/ style="text-transform: uppercase"></td>
</tr>
<td>Employment date (DD/MM/YYYY): </td>
<td>
<input type="text" name="edd" size="2" value="<?php echo $edd ?>" onKeyPress="return numbersonly(this, event)"/> /
<input type="text" name="emm" size="2" value="<?php echo $emm ?>" onKeyPress="return numbersonly(this, event)"/> /
<input type="text" name="eyyyy" size="4" value="<?php echo $eyyyy ?>" onKeyPress="return numbersonly(this, event)"/>
</td>
</tr>
<td>Title: </td>
<td><select name="title" size="6" />
	<option value="Prof. Dr." 			<?php if($title == "Prof. Dr.") {echo "selected"; } ?>> Prof. Dr.</option>
	<option value="Assoc. Prof. Dr." 	<?php if($title == "Assoc. Prof. Dr.") {echo "selected"; } ?>>Assoc. Prof. Dr.</option>
	<option value="Assist. Prof. Dr." 	<?php if($title == "Assist. Prof. Dr.") {echo "selected"; } ?>>Assist. Prof. Dr.</option>
	<option value="Dr." 				<?php if($title == "Dr.") {echo "selected"; } ?>>Dr.</option>
	<option value="Instr." 				<?php if($title == "Instr.") {echo "selected"; } ?>>Instr.</option>	
	<option value="Assistant" 			<?php if($title == "Assistant") {echo "selected"; } ?>>Assistant</option>
	<option value="Administrative" 		<?php if($title == "Administrative") {echo "selected"; } ?>>Administrative</option>
</td>
</tr>
</table>
<input type="submit" value="Submit" name="btnSubmit">
<input type="reset" value="Reset" name="btnReset">
</FORM>

<?php include 'bottom.php' ?>



</body>
</html>


